using UnityEngine;
using UnityEngine.UI;

public class ShopPanel : BasePanel
{
    [Header("商店数值")]
    public int damageCoin;
    public int armorCoin;
    public int spearOfBloodCoin;
    public int redKeyCoin;
    public int yellowKeyCoin;
    public int blueKeyCoin;
    public int hp1Coin;
    public int hp2Coin;
    public int damageNum;
    public int armorNum;
    public int spearOfBloodNum;
    public int redKeyNum;
    public int yellowKeyNum;
    public int blueKeyNum;
    public int hp1Num;
    public int hp2Num;

    [Header("数量")]
    public Text damageNumText;
    public Text armorNumText;
    public Text spearOfBloodNumText;
    public Text redKeyNumText;
    public Text yellowKeyNumText;
    public Text blueKeyNumText;
    public Text hp1Text;
    public Text hp2Text;
    [Header("金币")]
    public Text damageCoinText;
    public Text armorCoinText;
    public Text spearOfBloodCoinText;
    public Text redKeyCoinText;
    public Text yellowKeyCoinText;
    public Text blueKeyCoinText;
    public Text hp1CoinText;
    public Text hp2CoinText;
    [Header("购买按钮")]
    public Button damageButton;
    public Button armorButton;
    public Button spearOfBloodButton;
    public Button redKeyButton;
    public Button yellowKeyButton;
    public Button blueKeyButton;
    public Button hp1Button;
    public Button hp2Button;
    [Header("返回按钮")]
    public Button closeButton;
    public Text shopName;

    public override void OnClose()
    {
        base.OnClose();

        //退出可以进行角色操作
        GameManager.Instance.canOprate=true;
    }

    public override void OnInit()
    {
        base.OnInit();
        path="UI/Panel/ShopPanel";
        type=PanelType.Panel;

        //进入无法进行角色操作
        GameManager.Instance.canOprate=false;
    }

    public override void OnShow(params object[] par)
    {
        base.OnShow(par);
        //选择商店
        string levelStr = (string)par[0];
        switch (levelStr)
        {
            case "A": SetAShop(); break;
            case "B": SetBShop(); break;
            case "C": SetCShop(); break;
            case "D": SetDShop(); break;
        }
        //找到UI
        SeekUI(levelStr);
        //更新数据
        UpdateData();
    }
    //找到UI
    public void SeekUI(string str)
    {
        //UI
        damageCoinText=item.transform.Find("Item/DamageNum/Coin").GetComponent<Text>();
        damageNumText=item.transform.Find("Item/DamageNum").GetComponent<Text>();
        damageButton=item.transform.Find("Item/DamageNum/Buy").GetComponent<Button>();
        armorCoinText=item.transform.Find("Item/ArmorNum/Coin").GetComponent<Text>();
        armorNumText=item.transform.Find("Item/ArmorNum").GetComponent<Text>();
        armorButton=item.transform.Find("Item/ArmorNum/Buy").GetComponent<Button>();
        spearOfBloodCoinText=item.transform.Find("Item/SpearOfBlood/Coin").GetComponent<Text>();
        spearOfBloodNumText=item.transform.Find("Item/SpearOfBlood").GetComponent<Text>();
        spearOfBloodButton=item.transform.Find("Item/SpearOfBlood/Buy").GetComponent<Button>();
        redKeyCoinText=item.transform.Find("Item/RedKey/Coin").GetComponent<Text>();
        redKeyNumText=item.transform.Find("Item/RedKey").GetComponent<Text>();
        redKeyButton=item.transform.Find("Item/RedKey/Buy").GetComponent<Button>();
        yellowKeyCoinText=item.transform.Find("Item/YellowKey/Coin").GetComponent<Text>();
        yellowKeyNumText=item.transform.Find("Item/YellowKey").GetComponent<Text>();
        yellowKeyButton=item.transform.Find("Item/YellowKey/Buy").GetComponent<Button>();
        blueKeyCoinText=item.transform.Find("Item/BlueKey/Coin").GetComponent<Text>();
        blueKeyNumText=item.transform.Find("Item/BlueKey").GetComponent<Text>();
        blueKeyButton=item.transform.Find("Item/BlueKey/Buy").GetComponent<Button>();
        hp1CoinText = item.transform.Find("Item/Hp1/Coin").GetComponent<Text>();
        hp1Text = item.transform.Find("Item/Hp1").GetComponent<Text>();
        hp1Button = item.transform.Find("Item/Hp1/Buy").GetComponent<Button>();
        hp2CoinText = item.transform.Find("Item/Hp2/Coin").GetComponent<Text>();
        hp2Text = item.transform.Find("Item/Hp2").GetComponent<Text>();
        hp2Button = item.transform.Find("Item/Hp2/Buy").GetComponent<Button>();
        closeButton =item.transform.Find("Close").GetComponent<Button>();
        shopName=item.transform.Find("Name").GetComponent<Text>();

        //查看是否已经有血之矛
        if(ArtifactManager.instance.SeekHave("血之矛"))
        {
            spearOfBloodButton.gameObject.SetActive(false);
        }
        //按钮赋值
        closeButton.onClick.AddListener(CloseButton);
        damageButton.onClick.AddListener(BuyDamage);
        armorButton.onClick.AddListener(BuyArmor);
        spearOfBloodButton.onClick.AddListener(BuySpearOfBlood);
        redKeyButton.onClick.AddListener(BuyRedKey);
        yellowKeyButton.onClick.AddListener(BuyYellowKey);
        blueKeyButton.onClick.AddListener(BuyBlueKey);
        hp1Button.onClick.AddListener(BuyHp1);
        hp2Button.onClick.AddListener(BuyHp2);
        //数据赋值
        if(str=="B")
        {
            shopName.text = "百宝小鼠";
        }
        else if(str=="A")
        {
            shopName.text = "百宝鼠兄";
        }
    }
    //A商店
    public void SetAShop()
    {
        damageCoin=3;
        armorCoin=3;
        spearOfBloodCoin=20;
        redKeyCoin=3;
        yellowKeyCoin=3;
        blueKeyCoin=3;
        hp1Coin = 1;
        hp2Coin = 3;
        damageNum =3;
        armorNum=3;
        spearOfBloodNum=1;
        redKeyNum=3;
        yellowKeyNum=3;
        blueKeyNum=3;
        hp1Num = 50;
        hp2Num = 200;
    }
    //B商店
    public void SetBShop()
    {
        damageCoin=2;
        armorCoin=2;
        spearOfBloodCoin=20;
        redKeyCoin=3;
        yellowKeyCoin=3;
        blueKeyCoin=3;
        hp1Coin = 1;
        hp2Coin = 2;
        damageNum=1;
        armorNum=1;
        spearOfBloodNum=1;
        redKeyNum=1;
        yellowKeyNum=1;
        blueKeyNum=1;
        hp1Num = 10;
        hp2Num = 30;
    }
    //C商店
    public void SetCShop()
    {
        damageCoin=1;
        armorCoin=1;
        spearOfBloodCoin=20;
        redKeyCoin=1;
        yellowKeyCoin=1;
        blueKeyCoin=1;
        damageNum=1;
        armorNum=1;
        spearOfBloodNum=1;
        redKeyNum=1;
        yellowKeyNum=1;
        blueKeyNum=1;
    }
    //D商店
    public void SetDShop()
    {
        damageCoin=1;
        armorCoin=1;
        spearOfBloodCoin=20;
        redKeyCoin=1;
        yellowKeyCoin=1;
        blueKeyCoin=1;
        damageNum=1;
        armorNum=1;
        spearOfBloodNum=1;
        redKeyNum=1;
        yellowKeyNum=1;
        blueKeyNum=1;
    }
    //关闭
    public void CloseButton()
    {
        Close();

        AudManager.instance.PlayUIAud(AudManager.instance.uiDic["点击"], 1);
    }
    //购买伤害
    public void BuyDamage()
    {
        //查看当前金币是否足够
        if(GameManager.Instance.ReCoin(damageCoin))
        {
            GameManager.Instance.ChangeDamage(damageNum);
            TipSayManager.Instance.Say("<color=yellow>攻击力 +"+damageNum+"</color>");
        }

        AudManager.instance.PlayUIAud(AudManager.instance.uiDic["点击"], 1);
    }
    //购买防御
    public void BuyArmor()
    {
        //查看当前金币是否足够
        if (GameManager.Instance.ReCoin(armorCoin))
        {
            GameManager.Instance.ChangeArmor(armorNum);
            TipSayManager.Instance.Say("<color=yellow>防御力 +"+armorNum+"</color>");
        }

        AudManager.instance.PlayUIAud(AudManager.instance.uiDic["点击"], 1);
    }
    //购买血之矛
    public void BuySpearOfBlood()
    {
        //查看当前金币是否足够
        if (GameManager.Instance.ReCoin(spearOfBloodCoin))
        {
            ArtifactManager.instance.AddArtifact("血之矛");
            TipSayManager.Instance.Say("<color=yellow>血之矛 +"+spearOfBloodNum+"</color>");
            //关闭购买选项
            spearOfBloodButton.gameObject.SetActive(false);
        }

        AudManager.instance.PlayUIAud(AudManager.instance.uiDic["点击"], 1);
    }
    //购买红钥匙
    public void BuyRedKey()
    {
        //查看当前金币是否足够
        if (GameManager.Instance.ReCoin(redKeyCoin))
        {
            GameManager.Instance.AddKey(redKeyNum, "红色");
            TipSayManager.Instance.Say("<color=yellow>红钥匙 +"+redKeyNum+"</color>");
        }

        AudManager.instance.PlayUIAud(AudManager.instance.uiDic["点击"], 1);
    }
    //购买黄色钥匙
    public void BuyYellowKey()
    {
        //查看当前金币是否足够
        if (GameManager.Instance.ReCoin(yellowKeyCoin))
        {
            GameManager.Instance.AddKey(yellowKeyNum, "黄色");
            TipSayManager.Instance.Say("<color=yellow>黄钥匙 +"+yellowKeyNum+"</color>");
        }

        AudManager.instance.PlayUIAud(AudManager.instance.uiDic["点击"], 1);
    }
    //购买蓝色钥匙
    public void BuyBlueKey()
    {
        //查看当前金币是否足够
        if (GameManager.Instance.ReCoin(blueKeyCoin))
        {
            GameManager.Instance.AddKey(blueKeyNum, "蓝色");
            TipSayManager.Instance.Say("<color=yellow>蓝钥匙 +"+blueKeyNum+"</color>");
        }

        AudManager.instance.PlayUIAud(AudManager.instance.uiDic["点击"], 1);
    }
    //购买HP1
    public void BuyHp1()
    {
        //查看当前金币是否足够
        if (GameManager.Instance.ReCoin(hp1Coin))
        {
            GameManager.Instance.ChangePlayerHp(hp1Num);
            TipSayManager.Instance.Say("<color=yellow>血量 +" + hp1Num + "</color>");
        }

        AudManager.instance.PlayUIAud(AudManager.instance.uiDic["点击"], 1);
    }
    //购买HP2
    public void BuyHp2()
    {
        //查看当前金币是否足够
        if (GameManager.Instance.ReCoin(hp2Coin))
        {
            GameManager.Instance.ChangePlayerHp(hp2Num);
            TipSayManager.Instance.Say("<color=yellow>血量 +" + hp2Num + "</color>");
        }

        AudManager.instance.PlayUIAud(AudManager.instance.uiDic["点击"], 1);
    }
    //更新数据
    public void UpdateData()
    {
        damageNumText.text="+ "+damageNum;
        armorNumText.text="+ "+armorNum;
        spearOfBloodNumText.text="+ "+spearOfBloodNum;
        redKeyNumText.text="+ "+redKeyNum;
        blueKeyNumText.text="+ "+blueKeyNum;
        yellowKeyNumText.text="+ "+yellowKeyNum;
        hp1Text.text = "+ " + hp1Num;
        hp2Text.text = "+ " + hp2Num;

        damageCoinText.text=damageCoin.ToString();
        armorCoinText.text=armorCoin.ToString();
        spearOfBloodCoinText.text=spearOfBloodCoin.ToString();
        redKeyCoinText.text=redKeyCoin.ToString();
        blueKeyCoinText.text=blueKeyCoin.ToString();
        yellowKeyCoinText.text=yellowKeyCoin.ToString();
        hp1CoinText.text = hp1Coin.ToString();
        hp2CoinText.text = hp2Coin.ToString();
    }
}
